package org.smartercontext.contextmger.contextsphere;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

public class ContextManagerController {

	private static String ontSchemaGR;
	private static String nsSchemaGR;
	private static String ontSchemaShopping;
	private static String nsSchemaShopping;
	private static String ontSchemaPERSONAL;
	private static String nsSchemaPERSONAL;
	private static String ontSchemaGC;
	private static String nsSchemaGC;
	private static String rdfSchema;
	private static String rdf;
	private static String owl;
	private static Map<String, String> mappings;
	private static String nsUserModel;
	private static OntModel schemaModel;
	private static String nsCitiesModel;
	
	
	public ContextManagerController(){
		
	}
	
	public static void contextManagerController(){
		
		ontSchemaGR = "http://purl.org/goodrelations/v1";
		nsSchemaGR = ontSchemaGR + "#";
		
		ontSchemaShopping="http://smartercontext.org/vocabularies/shopping/v0.1/shopping.owl";
		nsSchemaShopping = ontSchemaShopping + "#";
		
		ontSchemaPERSONAL="http://smartercontext.org/vocabularies/personal/v0.1/personal.owl";
		nsSchemaPERSONAL = ontSchemaPERSONAL + "#";
		
		ontSchemaGC="http://smartercontext.org/vocabularies/gc/v0.1/gc.owl";
		nsSchemaGC = ontSchemaGC + "#";
		
		rdfSchema = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
		
		rdf = "http://www.w3.org/2002/07/owl#";
		
		owl = "http://www.w3.org/2002/07/owl#";
		mappings = new HashMap<String,String>();
		mappings.put("rdf", rdf);
		mappings.put("rdfs", rdfSchema);
		mappings.put("owl",owl);
		mappings.put("shopping", nsSchemaShopping);
		mappings.put("personal", nsSchemaPERSONAL);
		mappings.put("gc", nsSchemaGC);
		mappings.put("gr", nsSchemaGR);
		
		nsCitiesModel = "http://smartercontext.org/vocabularies/rdf/geo.rdf#";
		schemaModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		schemaModel.setNsPrefixes(mappings);
		
	}
	
	
	
	
	public static Model createUserModel(String username, String email, String name){
		
		contextManagerController();
		
		nsUserModel = "http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		
		Model user = ModelFactory.createDefaultModel();
		user.setNsPrefixes(mappings);
		
		Resource baseResource = user.createResource(nsUserModel+username);
		Property rdfType = user.createProperty(rdf,"type");
		
		Property personalType = user.createProperty(nsSchemaPERSONAL,"User");
		
		Literal emailUser = user.createTypedLiteral(new String(email));
		Property userEmali = user.createProperty(nsSchemaPERSONAL, "emailAccount");
		
		Literal nameUser = user.createTypedLiteral(new String(name));
		Property nameUseri = user.createProperty(nsSchemaPERSONAL, "givenName");
		
		user.addLiteral(baseResource, userEmali, emailUser);
		user.addLiteral(baseResource, nameUseri, nameUser);
		user.add(baseResource, rdfType, personalType);
		
		
		
		return user;
	}
	
	public static byte[] transformModelToBytes (Model model) throws Exception{
		
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		model.write(output);
		
		return output.toByteArray();
	}
	
	public static Model readModelFromBytes(byte[] model) throws Exception{
		
		ByteArrayInputStream input = new ByteArrayInputStream(model);
		
		Model modelx = ModelFactory.createDefaultModel();
		modelx.read(input, "RDF/XML");
		
		return modelx;
	}
	
	
	
	public static void integrateContext(Model model, String username)
	throws Exception{
		
		byte[]modelByte = transformModelToBytes(model);
		ContextSphereController csc = new ContextSphereController();
		csc.modifyContext(modelByte, username);
		
	}
	
	public static Model getUsermodel(String username)throws Exception{
		
		ContextSphereController csc = new ContextSphereController();
		byte[]rdfGraph = csc.getRdfGraph(username);
		
		if(rdfGraph!=null){
			Model userModel = readModelFromBytes(rdfGraph);
			return userModel;
		}
		return null;
		
	}

	public static String getOntSchemaGR() {
		return ontSchemaGR;
	}

	public static String getNsSchemaGR() {
		return nsSchemaGR;
	}

	public static String getOntSchemaShopping() {
		return ontSchemaShopping;
	}

	public static String getNsSchemaShopping() {
		return nsSchemaShopping;
	}

	public static String getOntSchemaPERSONAL() {
		return ontSchemaPERSONAL;
	}

	public static String getNsSchemaPERSONAL() {
		return nsSchemaPERSONAL;
	}

	public static String getOntSchemaGC() {
		return ontSchemaGC;
	}

	public static String getNsSchemaGC() {
		return nsSchemaGC;
	}

	public static String getRdfSchema() {
		return rdfSchema;
	}

	public static String getRdf() {
		return rdf;
	}

	public static String getOwl() {
		return owl;
	}

	public static Map<String, String> getMappings() {
		return mappings;
	}

	public static String getNsUserModel() {
		return nsUserModel;
	}

	public static OntModel getSchemaModel() {
		return schemaModel;
	}

	public static String getNsCitiesModel() {
		return nsCitiesModel;
	}
	
	
	
	
}
